{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from seq2seq.tools.inference import Translator\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "cuda = False\n",
    "checkpoint = torch.load('../results/en_he_dual_resumed/checkpoint.pth.tar')\n",
    "model = checkpoint['model']\n",
    "src_tok, target_tok = checkpoint['tokenizers'].values()\n",
    "\n",
    "translation_model = Translator(model,\n",
    "                               src_tok=src_tok,\n",
    "                               target_tok=target_tok,\n",
    "                               beam_size=5,\n",
    "                               length_normalization_factor=0.45,\n",
    "                               cuda=cuda)\n",
    "\n",
    "def translate(s, src_lang, target_lang, target_priming=None):\n",
    "    translation_model.set_src_language(src_lang)\n",
    "    translation_model.set_target_language(target_lang)\n",
    "    pred = translation_model.translate(s, target_priming=target_priming)\n",
    "    print(pred)\n",
    "\n",
    "def en2he(s, target_priming=None):\n",
    "    translate(s, 'en', 'he', target_priming=target_priming)\n",
    "    \n",
    "def he2en(s, target_priming=None):\n",
    "    translate(s, 'he', 'en', target_priming=target_priming)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world.\n"
     ]
    }
   ],
   "source": [
    "he2en('שלום עולם')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "How are you this morning?\n"
     ]
    }
   ],
   "source": [
    "he2en('מה שלומך הבוקר?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "שלום עולם העולם\n"
     ]
    }
   ],
   "source": [
    "en2he('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "נראה שזה עובד טוב טוב.\n"
     ]
    }
   ],
   "source": [
    "en2he('This seem to be working good')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ייתכן שהכוח יהיה איתך.\n"
     ]
    }
   ],
   "source": [
    "en2he('may the force be with you')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "מה אתה עושה שם?\n"
     ]
    }
   ],
   "source": [
    "en2he('three dogs?',target_priming='מה אתה עושה')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
